package org.exa.springdemo2;


import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/res")
public class HelloController {
    @RequestMapping("/hello")
    public String hello(){

        return "hello,spring mvc";
    }
    @RequestMapping("/r1")
    public String r1(String name){
        return "name="+name;
    }
    @RequestMapping("/r2")
    public String r2(int age){
        return "age="+age;
    }
    @RequestMapping("/r3")
    public String r3(Integer age){
        return "age="+age;
    }
    @RequestMapping("/r4")
    public String r4(String name,Integer age){
        return "name="+name+" "+"age="+age;
    }
    @RequestMapping("/r5")
    public String r5(@RequestBody Student student){
        return "个人信息"+student;
    }
    @RequestMapping("/r6")
    public String r6(@RequestParam(value = "UserName",required = false) String name){
        return "name="+name;
    }
    @RequestMapping("/r7")
    public String r7(Integer[] array){
        return "数组为"+ Arrays.toString(array);
    }
    @RequestMapping("/r8")
    public String r8(@RequestParam List<Integer> list){
        return "集合为"+list.toString();
    }
    @RequestMapping("/r9/{num}")
    public String r9(@PathVariable("num") String num){
        return "num="+num;
    }
    @RequestMapping("/r10")
    public String r10(MultipartFile file){
        String originalFilename = file.getOriginalFilename();
        return "名字为"+originalFilename;
    }
    //获取Cookie的两种方式
    @RequestMapping("/r11")
    public String r11(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        StringBuilder builder=new StringBuilder();
        if(cookies!=null){
            for (Cookie cook:cookies){
                builder.append(cook.getName()+":"+cook.getValue());
            }
        }
        return "Cookie信息为"+builder;
    }
    @RequestMapping("/r12")
    public String r12(@CookieValue("cook") String cook){
        return "Cookie为"+cook;
    }
    //获取Session的方式

    //1.首先要设置session
    @RequestMapping("/r13")
    public String r13(HttpServletRequest request){
        HttpSession session = request.getSession();
        if (session!=null){
            session.setAttribute("name","大力水手");
        }
        return "设置成功";
    }
    @RequestMapping("/r14")
    public String r14(HttpServletRequest request){
        HttpSession session = request.getSession();
        String name=null;
        if (session!=null&&session.getAttribute("name")!=null){
            name = (String) session.getAttribute("name");
        }
        return "name="+name;
    }
    @RequestMapping("/r15")
    public String r15(@SessionAttribute("name") String name){
        return "Sessio.name="+name;
    }
    @RequestMapping("/r16")
    public String r16(HttpServletRequest request){
        String header = request.getHeader("User-Agent");
        return "User-Agent为"+header;
    }
    @RequestMapping("/r17")
    public String r17(){
        return "/index.html";
    }
    @RequestMapping("/r18")
    public HashMap<String,String> r18(){
        HashMap<String,String> map=new HashMap<>();
        map.put("k1","v1");
        map.put("k2","v1");
        map.put("k3","v1");
        map.put("k4","v1");
        return map;
    }
    @RequestMapping("/r19")
    public String r19(HttpServletResponse response){
        response.setStatus(418);
        return "修改状态码成功";
    }
}
